<?php


	function re_data($id){
		//return $id;
		if($id==0){
			return '全区';
		}
		$res=M('region')->where("id=$id")->find();
		return $res['region_name'];
	}
	/**
	 * 手机验证重复重复的返回0，没重复返回原来手机号码;{号码|chack_phone}
	 * @return [type] [description]
	 */
	function chack_phone($phone){
		$where['u_phone']=$phone;
		$res=M("user")->where($where)->count();
		if($res){
			return $phone;
		}
		else{
			return 0;
		}
	}
	/**
	 * [search_user description]
	 * @param  [type] $u_id [传入用户u_id]
	 * @return [type]       [description]
	 */
	 function search_user($u_id){
		$count = M('user')->where("p_id=$u_id")->count();
		return $count; 
	}

     /**
      * goods_id换取nickname
      * 
      */
     function re_diy_nick($g_id){
     	$where['u_id']=session('user.u_id');
     	$where['g_id']=$g_id;
     	$res=M('nick_name')->where($where)->getField('nick_name');
     	return $res;
     }


	/********************账号特性*************************/
	/**
	 * 名字特性u-为一层人数 u-n为一层人的第一个名称
	 * @return [type] [description]
	 */
	function re_u_n($id){
		$where['web_id']=$id;
		$res=M('web_user')->where($where)->find();
		if(!$res)return '0';
		if($res['web_link_data']=='-0-' || $res['web_link_data']==''){
			if(!$res['p_u_id']){
				return '1';
			}
			else{
				$where_user['u_id']=$res['p_u_id'];
				$res_u=M('user')->where($where_user)->find();
				if(!$res_u){
					return '2';
				}
				else{
					return $res_u['u_name'];
				}
			}
		}
		else{
			$arr=explode('-',$res['web_link_data']);
			$last_web_id['web_id']=$arr[count($arr)-2];
			$res_u=M('web_user')->where($last_web_id)->find();
			if(!$res_u){
				return '3';
			}
			else{
				return $res_u['web_name'];
			}
		}
	}
	function re_uu_n($id){
		$where['web_id']=$id;
		$res=M('web_user')->where($where)->find();
		if(!$res)return '0';
		if($res['web_link_data']=='-0-' || $res['web_link_data']==''){
			if(!$res['p_u_id']){
				return '1';
			}
			else{
				$where_user['u_id']=$res['p_u_id'];
				$res_u=M('user')->where($where_user)->find();
				if(!$res_u){
					return '2';
				}
				else{
					return $res_u['u_name'];
				}
			}
		}
		else{
			$arr=explode('-',$res['web_link_data']);
			$last_web_id['web_id']=$arr[count($arr)-3];
			if($last_web_id['web_id']==0){
				if(!$res['p_u_id']){
					return '4';
				}
				else{
					$where_user['u_id']=$res['p_u_id'];
					$res_u=M('user')->where($where_user)->find();
					if(!$res_u){
						return '5';
					}
					else{
						return $res_u['u_name'];
					}
				}
			}
			else{
				$res_u=M('web_user')->where($last_web_id)->find();
				if(!$res_u){
					return '6';
				}
				else{
					return $res_u['web_name'];
				}
			}
		}
	}
	function re_d($id){
		$where['p_id']=$id;
		$count=M('web_user')->where($where)->count();
		if(!$count){
			return '没有推荐人';
		}
		else{
			return $count;
		}
	}

	function re_dd($id){
		$where['web_link_data']=array('like','%-'.$id.'-%');
		$res=M('web_user')->where($where)->field('web_link_data')->select();
		if($res=''){
			return '没有推荐人';
		}
		else{
	        $str=explode(',', $res['web_link_data']);
	        foreach ( $str as $key => $value) {
	            $str[$key]="%-$id-$value%-";
	        }
			$where_arr['web_link_data']=array('like',$str);
			$count=M('web_user')->where($where_arr)->count();
			return $count;
		}
	}
	/**
	 * 辅助导出
	 */
	function re_download($id){
		$where['p_id']=$id;
		$res=M('web_user')->where($where)->field('web_link_data')->select();
        $str=explode(',', $res['web_link_data']);
        foreach ( $str as $key => $value) {
            $str[$key]="%-$id-$value%-";
        }
		$where_arr['web_link_data']=array('like',$str);
		$userall=M('web_user')->where($where_arr)->select();
		foreach ($userall as $key => $value) {
			$userall[$key]['rand']='下下一级';
		}

		$where_0['p_id']=$id;
		$user=M('web_user')->where($where_0)->select();
		foreach ($user as $key => $value) {
			$user[$key]['rand']='下一级';
		}
		$rall=array_merge($userall,$user);
		return $rall;
	}
	/********************账号特性*************************/

	/******************地区账号公众号数据特性*************************/
	/**
	 * 获取公众号子账号数目
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function re_num_zi($id){
		$where['p_id']=$id;
		$res=M('user')->where($where)->count();
		return $res;
	}
	/**
	 * h获取公众号子账号下面的所有子账号按揭员、业务员总和
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function re_num_yj($id){
		$where_new['p_id']=$id;
		$res_user=M('user')->where($where_new)->select();
		$temp_count;
		$count;
		foreach ($res_user as $key => $value) {
			//循环所有子账号id
			$where['p_u_id']=$value['u_id'];
			$where['web_type']=array('in','0,1');
			$temp_count[$key]=M('web_user')->where($where)->count();
		}
		$count=0;
		for($i=0;$i<count($temp_count);$i++){
			$count=$count+$temp_count[$i];
		}
		return $count;

	}
	/**
	 * h获取公众号子账号下面的所有子账号银行职员总和
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function re_num_yh($id){
		$where_new['p_id']=$id;
		$res_user=M('user')->where($where_new)->select();
		$temp_count;
		$count;
		foreach ($res_user as $key => $value) {
			//循环所有子账号id
			$where['p_u_id']=$value['u_id'];
			$where['web_type']=2;
			$temp_count[$key]=M('web_user')->where($where)->count();
		}
		$count=0;
		for($i=0;$i<count($temp_count);$i++){
			$count=$count+$temp_count[$i];
		}
		return $count;
	}

	/*******************地区账号公众号数据特性************************/
	/**
	 * 获取子账号团队人数
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function re_num_ye($id){
		$where['p_u_id']=$id;
		$where['web_type']=array('in','0,1');
		$res=M('web_user')->where($where)->count();
		return $res;
	}
	/***********************add_adv_chanpin******************************/
	/**
	 * 标签id转换名称
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	function biaoqian_name($id){
		$where['id']=$id;
		$res=M('biaoqian')->where($where)->getField('name');
		return $res;
	}
	/**
	 * 核心信息id转换小名
	 * @param  [type] $g_id [description]
	 * @return [type]       [description]
	 */
	function re_litter_name($g_id){
		$where['id']=$g_id;
		$res=M('goods')->where($where)->getField('litter_name');
		return $res;
	}
	/***********************add_adv_chanpin******************************/
	function diy_qudao_name($type){
			switch ($type) {
				case '1':
				$my_type='银行';
				break;
				case '2':
				$my_type='小额贷款';
				break;
				case '3':
				$my_type='消费金融';
				break;
				case '4':
				$my_type='其他';
				break;
				default:
				$my_type='';
				break;
			}
			return $my_type;
	}